package servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "OEServlet", urlPatterns = "/OEServlet")
public class OEServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String orderID=req.getParameter("orderID");
        String RoomID=req.getParameter("RoomID");
        String checkin=req.getParameter("Checkin");
        String Now = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String checkYear=checkin.substring(0,3);int cy=Integer.parseInt(checkYear);
        String checkMonth=checkin.substring(4,5);int cm=Integer.parseInt(checkMonth);
        String checkDay=checkin.substring(6,7);int cd=Integer.parseInt(checkDay);
        String NowYear=Now.substring(0,3);int ny=Integer.parseInt(NowYear);
        String NowMonth=Now.substring(4,5);int nm=Integer.parseInt(NowMonth);
        String NowDay=Now.substring(6,7);int nd=Integer.parseInt(NowDay);
        if (cd-nd<=1){
            PrintWriter out=resp.getWriter();
            out.println("<html><body>");
            out.println("<h2>"+"距离入住时间不到一天，平台不予办理取消订单服务，如有疑问请联系客服"+"</h2>");
            out.println("<a href=OrderServlet>点击回到我的订单</a>");
            out.println("</body></html>");

        }

        Connection conn=null;
        Statement sql=null;
        String driver="org.mariadb.jdbc.Driver";
        String url="jdbc:mariadb://localhost:3306/hotel-management-system";
        System.out.println("订单修改页面23行"+orderID+RoomID);
//        int rs;
        try {
//            Connection con=null;
//            Statement sql;
            Class.forName(driver);
            conn= DriverManager.getConnection(url,"root","123456");
            sql=conn.createStatement();
            String orders="update orders set OrderCondition='已取消' where OrderID='"+orderID+"'" ;
            sql.executeQuery(orders);
            conn.close();
            } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
//        try {
////            Connection con=null;
////            Statement sql;
//            Class.forName(driver);
//            conn= DriverManager.getConnection(url,"root","123456");
//            String orders="update rooms set RoomCondi=1 where RoomID ='"+RoomID+"'" ;
//            sql=conn.createStatement();
//            sql.executeQuery(orders);
//            conn.close();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }

        resp.sendRedirect("OrderServlet");


    }
}
